package com.mike.employment.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.mike.employment.base.ApiResult;
import com.mike.employment.base.PageRequest;
import com.mike.employment.base.config.JwtConfig;
import com.mike.employment.base.exception.BusinessExcep;
import com.mike.employment.po.Account;
import com.mike.employment.service.AccountService;
import com.mike.employment.vo.AvatarVo;
import com.mike.employment.vo.LoginVo;

import jakarta.annotation.Resource;

@RestController
@RequestMapping("/api/account")
public class AccountController {
	
	public static final String separator = File.separator;
	
	@Value("${static.folder}")
    private String staticFolder;
	
	
	@Autowired
	private AccountService accountService;
	
	@Resource
	private JwtConfig jwtConfig;
	
	@PostMapping("/reg")
	public ApiResult register(@RequestBody LoginVo vo) {
		Account account = accountService.register(vo);
		return ApiResult.success(account);
	}

	@PostMapping("/login")
	public ApiResult login(@RequestBody LoginVo vo) {
		Account account = accountService.login(vo);
		Map<String, Object> result = new HashMap<>();
		result.put("account", account);
		result.put("token", jwtConfig.createToken(account.getId()));
		return ApiResult.success(result);
	}
	
	@PostMapping("/page")
	public ApiResult page(@RequestBody PageRequest req) {
		return ApiResult.success(accountService.getPage(req));
	}
	
	@PutMapping()
	public ApiResult saveOrUpdate(@RequestBody Account account) {
		return ApiResult.success(accountService.saveOrUpdate(account));
	}
	
	@DeleteMapping()
	public ApiResult delete(@RequestBody Long ...ids) {
		return ApiResult.success(accountService.delete(ids));
	}
	
	@GetMapping("/{id}")
	public ApiResult findAccount(@PathVariable("id") Long id) {
		return ApiResult.success(accountService.findById(id));
	}
	
	@PostMapping("/avatar")
	public ApiResult avatar(@ModelAttribute AvatarVo vo) {
		MultipartFile imgFile = vo.getImg();
		String newName = "";
		String sourcePath = System.getProperty("user.dir") + separator +"src"+separator+ "main"+separator+"resources"+separator+ staticFolder;
		if(imgFile != null && !imgFile.isEmpty()){
			try {
				String fileName = imgFile.getOriginalFilename();
				String timestamp = "" + new Date().getTime();		
				newName = timestamp + fileName;
				String storePath = sourcePath + separator + newName;
				BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(storePath)));
				out.write(imgFile.getBytes());
				out.flush();
				out.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				throw new BusinessExcep("修改头像失败");
			} catch (IOException e) {
				e.printStackTrace();
				throw new BusinessExcep("修改头像失败");
			}
		}
		Account ac = new Account();
		ac.setId(vo.getAccount());
		ac.setAvatar(newName);
		return ApiResult.success(accountService.saveOrUpdate(ac));
	}
}
